using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Data;

using GonzalesCore.CoreElements;
using Kypris2.CoreElements;
using Kypris2.CoreManagers;
using Jamila2.CoreElements;
using Jamila2.Tools;
using Jamila2.Database;

namespace GonzalesCore.CoreManagers
{
    public class CompanyManager : KyprisManagerBase, IManager, IDisposable
    {
        private GonzalesDataStructure DataStructrure = new GonzalesDataStructure();
        private PersonalSessionAccount CurrentModuleSession = null;

        public CompanyManager(PersonalSessionAccount PrivateConfig)
            : base(PrivateConfig)
        {
            this.CurrentModuleSession = PrivateConfig;
        }

        #region GetAllCompany
        public GonzalesCompanies GetAllCompany()
        {
            GonzalesCompanies ResultObj = null;

            DatabaseParameters KeyParameters = new DatabaseParameters();

            DataTable ResultTable = this.QueryData(KeyParameters);
            if (ResultTable != null)
            {
                ResultObj = new GonzalesCompanies();

                foreach (DataRow ResultRow in ResultTable.Rows)
                {
                    GonzalesCompany ResultCompany = this.LoadCompany(ResultRow);
                    ResultObj.Add(ResultCompany);
                }
            }

            return ResultObj;
        }
        #endregion


        #region GetCompanyByID
        public GonzalesCompany GetCompanyByID(string InternalID)
        {
            GonzalesCompany ResultObj = null;

            DatabaseParameters KeyParameters = new DatabaseParameters();
            KeyParameters.Add(new DatabaseParameter(this.DataStructrure.Views.MasterCompany.InternalID.ActualFieldName, InternalID));

            DataTable ResultTable = this.QueryData(KeyParameters);
            if (ResultTable != null && ResultTable.Rows.Count == 1)
            {
                ResultObj = this.LoadCompany(ResultTable.Rows[0]);
            }            

            return ResultObj;
        }
        #endregion

        #region GetCompanyByEntityID
        public GonzalesCompanies GetCompanyByEntityID(string EntityID)
        {
            GonzalesCompanies ResultObj = null;

            DatabaseParameters KeyParameters = new DatabaseParameters();
            KeyParameters.Add(new DatabaseParameter(this.DataStructrure.Views.MasterCompany.Entity.ActualFieldName, EntityID));

            DataTable ResultTable = this.QueryData(KeyParameters);
            if (ResultTable != null)
            {
                ResultObj = new GonzalesCompanies();

                foreach (DataRow ResultRow in ResultTable.Rows)
                {
                    GonzalesCompany ResultCompany = this.LoadCompany(ResultRow);
                    ResultObj.Add(ResultCompany);
                }
            }   

            return ResultObj;
        }
        #endregion

        #region UpdateCompany

        public bool UpdateCompany(GonzalesCompany CompanyObj)
        {
            bool smooth = true;

            DatabaseParameters KeyParameters = new DatabaseParameters();
            DatabaseParameters ValParameters = new DatabaseParameters();

            KeyParameters.Add(new DatabaseParameter(this.DataStructrure.Tables.MasterCompany.InternalID.ActualFieldName, CompanyObj.InternalID));
            ValParameters.Add(new DatabaseParameter(this.DataStructrure.Tables.MasterCompany.CompanyName.ActualFieldName, CompanyObj.CompanyName));
            ValParameters.Add(new DatabaseParameter(this.DataStructrure.Tables.MasterCompany.CompanyManager.ActualFieldName, CompanyObj.CompanyManager.InternalID));
            ValParameters.Add(new DatabaseParameter(this.DataStructrure.Tables.MasterCompany.MileageRate.ActualFieldName, CompanyObj.MileageRate.ToString()));
            ValParameters.Add(new DatabaseParameter(this.DataStructrure.Tables.MasterCompany.PettyCashLimit.ActualFieldName, CompanyObj.PettyCashLimit.ToString()));
            ValParameters.Add(new DatabaseParameter(this.DataStructrure.Tables.MasterCompany.IsRepOffice.ActualFieldName, ((CompanyObj.IsRepresentative)?"1":"0")));
            ValParameters.Add(new DatabaseParameter(this.DataStructrure.Tables.MasterCompany.CreditCardPaymentType.ActualFieldName, CreditCardManager.GetCreditCardPaymentTypeInCode(CompanyObj.CreditCardPaymentType).ToString() ));
            ValParameters.Add(new DatabaseParameter(this.DataStructrure.Tables.MasterCompany.CCActivateDay.ActualFieldName, CompanyObj.CreditCardActivatedDay.ToString()));
            ValParameters.Add(new DatabaseParameter(this.DataStructrure.Tables.MasterCompany.CCCutOffDay.ActualFieldName, CompanyObj.CreditCardCutOffDay.ToString()));
            ValParameters.Add(new DatabaseParameter(this.DataStructrure.Tables.MasterCompany.DefaultLDAP.ActualFieldName, CompanyObj.DefaultLDAP));
            ValParameters.Add(new DatabaseParameter(this.DataStructrure.Tables.MasterCompany.UsingVAT.ActualFieldName, (CompanyObj.UsingVAT ? "1" : "0")));
            ValParameters.Add(new DatabaseParameter(this.DataStructrure.Tables.MasterCompany.ExpiryDate.ActualFieldName, CompanyObj.ExpiryDate));

            //Added by Agnes[31march2009] - for enhancement
            ValParameters.Add(new DatabaseParameter(this.DataStructrure.Tables.MasterCompany.CreditCardReminder.ActualFieldName, CompanyObj.CreditCardReminder));
            ValParameters.Add(new DatabaseParameter(this.DataStructrure.Tables.MasterCompany.ClaimReminder.ActualFieldName, CompanyObj.ClaimReminder));


            if (this.TryConnection())
            {
                this.CurSQLFactory.UpdateCommand(KeyParameters, ValParameters, this.DataStructrure.Tables.MasterCompany.ActualTableName);
                if (!(smooth = this.CurDBEngine.ExecuteQuery(this.CurSQLFactory.SQL)))
                {
                    this.ErrMsg = "[CompanyManager.UpdateCompany] : this.CurDBEngine.ExecuteQuery('" + this.CurSQLFactory.SQL + "') : " + this.ErrMsg;
                }
            }
            else
            {
                smooth = false;
                this.ErrMsg = "[CompanyManager.UpdateCompany] : Exception : " + this.ErrMsg;
            }

            return smooth;
        }

        #endregion

        #region GetPaymentDayByCompany

        public int[] GetPaymentDayByCompany(string CompanyID)
        {
            int[] ResultPaymentDay = null;

            if (this.TryConnection())
            {
                DatabaseParameters KeyParameters = new DatabaseParameters();
                KeyParameters.Add(new DatabaseParameter(this.DataStructrure.Tables.MasterPaymentDate.CompanyID.ActualFieldName, CompanyID));

                this.CurSQLFactory.SelectCommand(KeyParameters, this.DataStructrure.Tables.MasterPaymentDate.ActualTableName);
                DataTable ResultTable = this.CurDBEngine.SelectQuery(this.CurSQLFactory.SQL);

                if (ResultTable != null)
                {
                    ResultPaymentDay = new int[ResultTable.Rows.Count];

                    DataRow[] ResultRows = ResultTable.Select("", this.DataStructrure.Tables.MasterPaymentDate.CutOffDay.ActualFieldName);

                    for (int x = 0; x < ResultRows.Length; x++)
                    {
                        ResultPaymentDay[x] = Convert.ToInt32(ResultRows[x][this.DataStructrure.Tables.MasterPaymentDate.CutOffDay.ActualFieldName]);
                    }
                }
                else
                {
                    this.ErrMsg = "[CompanyManager.GetPaymentDayByCompany] : Failed at this.CurDBEngine.SelectQuery('" + this.CurSQLFactory.SQL + "') : " + this.CurDBEngine.ErrorMessage;
                }
            }
            else
            {
                this.ErrMsg = "[CompanyManager.GetPaymentDayByCompany] : Exception : " + this.ErrMsg;
            }

            return ResultPaymentDay;

        }

        #endregion

        #region UpdatePaymentDay
        public bool UpdatePaymentDay(int[] PaymentDays, string CompanyID)
        {
            bool smooth = true;
            ArrayList sql = new ArrayList();

            if (this.TryConnection())
            {
                // clear data
                DatabaseParameters KeyParameters = new DatabaseParameters();
                KeyParameters.Add(new DatabaseParameter(this.DataStructrure.Tables.MasterPaymentDate.CompanyID.ActualFieldName, CompanyID));

                this.CurSQLFactory.DeleteCommand(KeyParameters, this.DataStructrure.Tables.MasterPaymentDate.ActualTableName);
                sql.Add(this.CurSQLFactory.SQL);

                // now insert new
                if (PaymentDays != null)
                {
                    for (int x = 0; x < PaymentDays.Length; x++)
                    {
                        KeyParameters.Clear();
                        KeyParameters.Add(new DatabaseParameter(this.DataStructrure.Tables.MasterPaymentDate.CompanyID.ActualFieldName, CompanyID));
                        KeyParameters.Add(new DatabaseParameter(this.DataStructrure.Tables.MasterPaymentDate.CutOffDay.ActualFieldName, PaymentDays[x].ToString()));

                        this.CurSQLFactory.InsertCommand(KeyParameters, this.DataStructrure.Tables.MasterPaymentDate.ActualTableName);
                        sql.Add(this.CurSQLFactory.SQL);
                    }
                }

                // real database operation
                if (!(smooth = this.CurDBEngine.ExecuteQuery(sql)))
                {
                    this.ErrMsg = "[CompanyManager.UpdatePaymentDay] : Failed at this.CurDBEngine.ExecuteQuery() : " + this.CurDBEngine.ErrorMessage;
                }
            }
            else
            {
                this.ErrMsg = "[CompanyManager.UpdatePaymentDay] : Exception : " + this.ErrMsg;
            }


            return smooth;
        }
        #endregion

        #region Protected

        #region LoadCompany
        protected GonzalesCompany LoadCompany(DataRow ResultRow)
        {
            GonzalesCompany ResultObj = null;

            if (ResultRow != null)
            {
                ResultObj = new GonzalesCompany(
                    ResultRow[this.DataStructrure.Views.MasterCompany.InternalID.ActualFieldName].ToString(),
                    ResultRow[this.DataStructrure.Views.MasterCompany.CompanyName.ActualFieldName].ToString());

                ResultObj.CompanyCode = ResultRow[this.DataStructrure.Views.MasterCompany.CompanyID.ActualFieldName].ToString();
                ResultObj.Entity = new Entity(
                    ResultRow[this.DataStructrure.Views.MasterCompany.Entity.ActualFieldName].ToString(), 
                    ResultRow[this.DataStructrure.Views.MasterCompany.EntityName.ActualFieldName].ToString());
                ResultObj.FromCountry = new Country(
                    ResultRow[this.DataStructrure.Views.MasterCompany.Country.ActualFieldName].ToString(), 
                    ResultRow[this.DataStructrure.Views.MasterCompany.CountryName.ActualFieldName].ToString());
                ResultObj.IsRepresentative = (ResultRow[this.DataStructrure.Views.MasterCompany.IsRepOffice.ActualFieldName].ToString().CompareTo("1") == 0);

                ResultObj.CompanyManager = new SystemUser(
                    ResultRow[this.DataStructrure.Views.MasterCompany.CompanyManager.ActualFieldName].ToString(),
                    ResultRow[this.DataStructrure.Views.MasterCompany.ManagerDisplayName.ActualFieldName].ToString());
                ResultObj.CompanyManager.ActiveDirectoryDistinguisedName = ResultRow[this.DataStructrure.Views.MasterCompany.ManagerActiveDirectoryDistingushedName.ActualFieldName].ToString();
                ResultObj.CompanyManager.Email = ResultRow[this.DataStructrure.Views.MasterCompany.ManagerEmail.ActualFieldName].ToString();
                ResultObj.CompanyManager.EmailID = ResultRow[this.DataStructrure.Views.MasterCompany.ManagerEmailID.ActualFieldName].ToString();
                ResultObj.CompanyManager.EmployeeID = ResultRow[this.DataStructrure.Views.MasterCompany.ManagerEmployeeID.ActualFieldName].ToString();

                ResultObj.MileageRate = Convert.ToDecimal(ResultRow[this.DataStructrure.Tables.MasterCompany.MileageRate.ActualFieldName]);
                ResultObj.PettyCashLimit = Convert.ToDecimal(ResultRow[this.DataStructrure.Tables.MasterCompany.PettyCashLimit.ActualFieldName]);
                ResultObj.CreditCardPaymentType = CreditCardManager.GetCreditCardPaymentTypeFromCode(Convert.ToInt32(ResultRow[this.DataStructrure.Tables.MasterCompany.CreditCardPaymentType.ActualFieldName]));

                ResultObj.CreditCardCutOffDay = Convert.ToInt32(ResultRow[this.DataStructrure.Tables.MasterCompany.CCCutOffDay.ActualFieldName]);
                ResultObj.CreditCardActivatedDay = Convert.ToInt32(ResultRow[this.DataStructrure.Tables.MasterCompany.CCActivateDay.ActualFieldName]);

                ResultObj.UsingVAT = (ResultRow[this.DataStructrure.Views.MasterCompany.UsingVAT.ActualFieldName].ToString().CompareTo("1") == 0);

                ResultObj.DefaultLDAP = ResultRow[this.DataStructrure.Views.MasterCompany.DefaultLDAP.ActualFieldName].ToString();

                ResultObj.ExpiryDate = ResultRow[this.DataStructrure.Views.MasterCompany.ExpiryDate.ActualFieldName].ToString();

                //added by agnes [ 31 march 2009] - for enhancement to send out the email
                ResultObj.CreditCardReminder = ResultRow[this.DataStructrure.Tables.MasterCompany.CreditCardReminder.ActualFieldName].ToString();
                ResultObj.ClaimReminder = ResultRow[this.DataStructrure.Tables.MasterCompany.ClaimReminder.ActualFieldName].ToString();

            }
            else
            {
                this.ErrMsg = "[CompanyManager.FillData] : ResultRow is null";
            }

            return ResultObj;
        }
        #endregion

        #region QueryData
        protected DataTable QueryData(DatabaseParameters KeyParameters)
        {
            return base.QueryData(KeyParameters, this.DataStructrure.Views.MasterCompany.ActualTableName);
        }
        #endregion

        #endregion
    }
}
